package top.anka1.fxgui;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Stage;

import top.anka1.ims.*;
/**
 * RestaurantController is responsible for handling the interactions between the Restaurant GUI and the underlying logic.
 * It provides methods to show various views related to restaurant operations such as adding ingredients, creating meals, etc.
 * @author Anka
 */
public class RestaurantController {
    private RestaurantGUI restaurantGUI = new RestaurantGUI();
    private BankingService bankingService = new BankingService();
    private Stage stage;
    private Stage stage2;

    public void setStage(Stage stage) {
        this.stage = stage;
    }
    public void setStage2(Stage stage2) {
        this.stage2 = stage2;
    }

    @FXML
    public void exitRestaurant() {
        Alert confirmationAlert = new Alert(Alert.AlertType.CONFIRMATION);
        confirmationAlert.setTitle("Exit Confirmation");
        confirmationAlert.setHeaderText("Are you sure you want to exit?");
        confirmationAlert.setContentText("Click OK to exit, or Cancel to return to the application.");
        confirmationAlert.showAndWait().ifPresent(response -> {
            if (response == javafx.scene.control.ButtonType.OK) {
                //System.exit(0);
                try {
                    stage.hide();
                } catch (Exception e) {
                    //e.printStackTrace();
                }
                try {
                    stage2.hide();
                } catch (Exception e) {
                    //e.printStackTrace();
                }
            }
        });
    }
    @FXML
    public void showCopyright() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("About_Copyright.fxml"));
            AboutCopyrightController aboutCopyrightController = fxmlLoader.getController();
            Stage cpoyRightStage = new Stage();
            cpoyRightStage.setScene(new Scene(fxmlLoader.load()));
            cpoyRightStage.setTitle("About");
            cpoyRightStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void addIngredient() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("addIngredient-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            AddIngredientController addIngredientController = fxmlLoader.getController();
            addIngredientController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("Add Ingredient");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void viewIngredient() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("displayIngredients-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            DisplayIngredientsController displayIngredientsController = fxmlLoader.getController();
            displayIngredientsController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("View Ingredient");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void removeIngredient() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("removeIngredients-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            RemoveIngredientsController removeIngredientsController = fxmlLoader.getController();
            removeIngredientsController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("Remove Ingredient");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void createMeal() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("createMeal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            CreateMealController createMealController = fxmlLoader.getController();
            createMealController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("Create Meal");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void removeMeal() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("removeMeal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            RemoveMealController removeMealController = fxmlLoader.getController();
            removeMealController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("Remove Meal");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void viewMeal() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("displayMeals-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            DisplayMealsController displayMealsController = fxmlLoader.getController();
            displayMealsController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("View All Created Meals");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void addMealToMenu() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("addMealToMenu-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            AddMealToMenuController addMealToMenuController = fxmlLoader.getController();
            Stage stage = new Stage();
            addMealToMenuController.setData(stage, restaurantGUI);
            stage.setScene(scene);
            stage.setTitle("Add Meal to Menu / Remove Meal from Menu");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void displayMenu() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("displayMenu-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            DisplayMenuController displayMenuController = fxmlLoader.getController();
            displayMenuController.setData(restaurantGUI);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("View Menu");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void addMealToOrder() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("addMealToOrder-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            AddMealToOrderController addMealToOrderController = fxmlLoader.getController();
            Stage stage = new Stage();
            addMealToOrderController.setData(restaurantGUI, stage);
            stage.setScene(scene);
            stage.setTitle("Add Meal to Order");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void viewOrder() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("displayOrder-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            DisplayOrderController displayOrdersController = fxmlLoader.getController();
            Stage stage = new Stage();
            displayOrdersController.setData(restaurantGUI, stage);
            stage.setScene(scene);
            stage.setTitle("View Order");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void generateBill() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(RestaurantApplication.class.getResource("generateBill-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            GenerateBillController generateBillController = fxmlLoader.getController();
            Stage stage = new Stage();
            generateBillController.setData(restaurantGUI, bankingService, stage);
            stage.setScene(scene);
            stage.setTitle("Generate Bill");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
